package com.jie.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jie.eduservice.entity.EduChapter;
import com.jie.eduservice.entity.EduVideo;
import com.jie.eduservice.entity.chapter.ChapterVO;
import com.jie.eduservice.entity.chapter.VideoVO;
import com.jie.eduservice.mapper.EduChapterMapper;
import com.jie.eduservice.mapper.EduVideoMapper;
import com.jie.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jie.servicebase.exception.GuLiException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author xzj
 * @since 2020-10-09
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    // 注入小节的mapper
    private final EduVideoMapper eduVideoMapper;

    @Autowired
    public EduChapterServiceImpl(EduVideoMapper eduVideoMapper) {
        this.eduVideoMapper = eduVideoMapper;
    }

    // 我与老师的做法不一样
    @Override
    public List<ChapterVO> getChapterVideoByCourseId(String courseId) {

        // 1.根据课程id查询课程里的所有章节
        QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id", courseId);
        List<EduChapter> eduChapters = baseMapper.selectList(chapterQueryWrapper);

        // 2.根据课程id查询课程里的所有小节
        QueryWrapper<EduVideo> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id", courseId);
        List<EduVideo> eduVideos = eduVideoMapper.selectList(videoQueryWrapper);

        // 3.遍历查询章节list集合进行封装
        List<ChapterVO> finalList = new ArrayList<>();
        Map<String, ChapterVO> map = new HashMap<>();
        for (EduChapter eduChapter : eduChapters) {
            ChapterVO chapterVO = new ChapterVO();
            BeanUtils.copyProperties(eduChapter, chapterVO);
            finalList.add(chapterVO);
            map.put(eduChapter.getId(), chapterVO);  // 用于小节能快速找到属于自己的章节
        }

        // 4.遍历查询小节的list集合, 进行封装
        for (EduVideo eduVideo : eduVideos) {
            VideoVO videoVO = new VideoVO();
            BeanUtils.copyProperties(eduVideo, videoVO);
            ChapterVO tempChapter = map.get(eduVideo.getChapterId());
            if (tempChapter != null) {
                tempChapter.getChildren().add(videoVO);
            }
        }

        return finalList;
    }

    @Override
    public boolean deleteChapter(String chapterId) {
        // 根据chapter章节id查询小节表, 如果大于0则不允许删除该章节
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        int count = eduVideoMapper.selectCount(wrapper);

        if (count > 0) {  // 该章节下有小节, 不进行删除
            throw new GuLiException(20001, "该章节下面还有小节, 无法删除!");  // 感觉可以不用抛异常, 直接返回false也可以
        }
        // 删除章节
        int result = baseMapper.deleteById(chapterId);
        return result > 0;
    }

    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        baseMapper.delete(wrapper);
    }

}
